package com.busionline.wotu.baselibrary.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * 项目名称：joke
 * 类描述：（仅在包内使用）
 * 创建人：chenwuchao
 * 创建时间：2017/5/9 15:03
 */
class AlertController {

    private final AlertDialog mDialog;
    private final Window window;

    public void setViewHelper(DialogViewHelper viewHelper) {
        this.viewHelper = viewHelper;
    }

    private DialogViewHelper viewHelper;

    public AlertController(AlertDialog dialog, Window window) {
        this.mDialog = dialog;
        this.window = window;
    }

    /**
     * 设置文本
     * @param resId
     * @param charSequence
     */
    public void setText(int resId, CharSequence charSequence) {
        viewHelper.setText(resId,charSequence);
    }

    /**
     * 在集合中通过id找到控件，没有就保存进集合中
     * @return
     * @param resId
     */
    public  <T extends View>T getView(int resId) {
        return viewHelper.getView(resId);
    }

    /**
     * 设置点击事件
     * @param resId
     * @param onClickListener
     */
    public void setOnClickListener(int resId, View.OnClickListener onClickListener) {
        viewHelper.setOnClickListener(resId,onClickListener);
    }



    /**
     * 获取diaglog
     *
     * @return
     */
    public AlertDialog getDialog() {
        return mDialog;
    }

    /**
     * 获取diaglog的window
     *
     * @return
     */
    public Window getWindow() {
        return window;
    }

    public static class AlertParams {

        public final Context mContext;
        public final int mThemeResId;
        //默认点击空白可以取消
        public boolean mCancelable = true;
        //取消监听
        public DialogInterface.OnCancelListener mOnCancelListener;
        //销毁监听
        public DialogInterface.OnDismissListener mOnDismissListener;
        //键盘按下监听
        public DialogInterface.OnKeyListener mOnKeyListener;
        //dialog布局
        public View mView;
        //dialog布局id
        public int mViewLayoutResId;

        //文本集合保存
        public SparseArray<CharSequence> textArrays = new SparseArray<>();
        //点击事件保存
        public SparseArray<View.OnClickListener> clickArrays = new SparseArray<>();
        //diaglog布局处理类
        private DialogViewHelper viewHelper;
        //宽度
        public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
        //高度
        public int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
        //位置
        public int mGravity = Gravity.CENTER;
        public int mAnimation = 0;


        public AlertParams(Context context, int themeResId) {
            this.mContext = context;
            this.mThemeResId = themeResId;
        }

        /**
         * 绑定参数
         *
         * @param mAlert
         */
        public void apply(AlertController mAlert) {
            //1.设置布局
            if (mViewLayoutResId != 0) {
                //开发者传入了资源id
                viewHelper = new DialogViewHelper(mContext,mViewLayoutResId);
            }
            if(mView != null){
                //开发者传入了视图
                viewHelper = new DialogViewHelper();
                viewHelper.setContentView(mView);
            }
            if(viewHelper == null){
                //设置布局方法没有调用
                throw  new IllegalArgumentException("请设置布局setContentView");
            }
            //给dialog设置布局
            mAlert.getDialog().setContentView(viewHelper.getContentView());

            //设置viewHelper
            mAlert.setViewHelper(viewHelper);

            //2.设置文本
            for (int i = 0; i < textArrays.size(); i++) {
                viewHelper.setText(textArrays.keyAt(i),textArrays.valueAt(i));
            }
            //3.设置点击事件
            for (int i = 0; i < clickArrays.size(); i++) {
                viewHelper.setOnClickListener(clickArrays.keyAt(i),clickArrays.valueAt(i));
            }
            //4.设置自定义效果
            Window window = mAlert.getWindow();
            //设置位置
            window.setGravity(mGravity);
            //设置动画
            if(mAnimation != 0){
                window.setWindowAnimations(mAnimation);
            }
            //设置宽高
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width = mWidth;
            attributes.height = mHeight;
            window.setAttributes(attributes);
        }
    }
}
